package jsyf.oa.service.intentCustomerReport;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jsyf.com.util.FastJsonConvert;
import jsyf.oa.dao.intentCustomerReport.IntentCustomerReportDao;
import jsyf.oa.entity.report.mineCustomer.CustomerReportEntity;
import jsyf.oa.facade.intentCustomerReport.IntentCustomerReportFacade;
import jsyf.oa.service.report.reportCommon.ReportCommonMethodsAndConstant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by 92852 on 2018/8/7.
 */
@Service
@Transactional(rollbackFor = Exception.class)
@com.alibaba.dubbo.config.annotation.Service(protocol = {"dubbo"})
public class IntentCustomerReportService implements IntentCustomerReportFacade {
    @Autowired
    private IntentCustomerReportDao intentcustomerreportDao;
    @Autowired
    private ReportCommonMethodsAndConstant common;

    @Override
    public  List<JSONObject> getGroupByUserId(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String userId = jsonParams.getString("userId");
        String companyId = common.getAllCompanysIdByUserId(userId);
        List<JSONObject> list=intentcustomerreportDao.getGroupByUserId(companyId);
        return list;
    }

    @Override
    public JSONObject getAllCustomersByCompanyId(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String companyId = jsonParams.getString("companyId");
        String key = jsonParams.getString("key");
        StringBuffer condition=new StringBuffer();
        if (StringUtils.isNotBlank(companyId) && StringUtils.isNotEmpty(companyId)) {
            condition.append("AND t_sys_company.ID='" + companyId + "'\n" );
        }
        if (StringUtils.isNotBlank(key) && StringUtils.isNotEmpty(key)) {
            condition.append("AND (t_wrt_customer.`NAME` LIKE '%"+key+"%'\n" +
                    "OR t_wrt_customer.TEL LIKE '%"+key+"%'\n" + ")\n" );
        }
        /*分页信息*/
        int requestPage = 1;
        int pageSize = Integer.MAX_VALUE;
        if (jsonParams.containsKey("pageSize")) {
            pageSize = jsonParams.getInteger("pageSize");
        }
        if (jsonParams.containsKey("requestPage")) {
            requestPage = jsonParams.getInteger("requestPage");
        }
        List<JSONObject> list=intentcustomerreportDao.getAllCustomersByCompanyId(condition.toString(),requestPage, pageSize);
        int recordCount = intentcustomerreportDao.getCountGroupCustomerList(condition.toString());
        //如果整除表示正好分N页，如果不能整除在N页的基础上+1页
        int pageCount = recordCount % pageSize == 0 ? recordCount / pageSize : (recordCount / pageSize) + 1;
        //存放分页信息
        JSONObject sendData = new JSONObject();
        //存放list
        JSONObject result = new JSONObject();
        sendData.put("recordCount", recordCount);
        sendData.put("pageCount", pageCount);
        sendData.put("requestPage", 1);
        result.put("pager", sendData);
        // 子数组对象
        result.put("records", list);
        return result;
    }

    @Override
    public JSONObject getPersonalByUserId(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String userId = jsonParams.getString("userId");
        String key = jsonParams.getString("key");
        StringBuffer condition=new StringBuffer();
        if (StringUtils.isNotBlank(userId) && StringUtils.isNotEmpty(userId)) {
            condition.append("AND t_wrt_customer.CREATE_USER_ID='" + userId + "'\n" );
        }
        if (StringUtils.isNotBlank(key) && StringUtils.isNotEmpty(key)) {
            condition.append("AND (t_wrt_customer.`NAME` LIKE '%"+key+"%'\n" +
                    "OR t_wrt_customer.TEL LIKE '%"+key+"%'\n" + ")\n" );
        }
        List<JSONObject> list=intentcustomerreportDao.getPersonalByUserId(condition.toString());
        JSONObject result = new JSONObject();
        result.put("records",list);
        return result;
    }

    @Override
    public JSONObject getBigRegionByUserId(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String userId = jsonParams.getString("userId");
        String key = jsonParams.getString("key");
        StringBuffer condition=new StringBuffer();
        if (StringUtils.isNotBlank(userId) && StringUtils.isNotEmpty(userId)) {
            condition.append("AND( t_sys_user.ID='"+userId+"'\n" +
                    "OR t_sys_user.PARENT_ID='"+userId+"'\n" +
                    "OR (\n" +
                    "\t\t\tt_sys_user.ID in(\n" +
                    "\t\t\t\t\t\t\tSELECT USER_ID FROM t_sys_user_2_region \n" +
                    "\t\t\t\t\t\t\tWHERE \n" +
                    "\t\t\t\t\t\t\tREGION_ID in(\n" +
                    "\t\t\tSELECT REGION_ID FROM t_sys_user_2_region WHERE USER_ID = '"+userId+"'\n" +
                    "\t)\n" +
                    "\t\t\t\t\t\t\t\t\t)\n" +
                    "\t\t\t\t\tAND t_sys_role_name.FRONT_FLAG = 1\n" +
                    "\t\t\t\t)\n" +
                    ")\n" );
        }
        if (StringUtils.isNotBlank(key) && StringUtils.isNotEmpty(key)) {
            condition.append("AND (t_wrt_customer.`NAME` LIKE '%"+key+"%'\n" +
                    "OR t_wrt_customer.TEL LIKE '%"+key+"%'\n" + ")\n" );
        }
        List<JSONObject> list=intentcustomerreportDao.getBigRegionByUserId(condition.toString());
        JSONObject result = new JSONObject();
        result.put("records",list);
        return result;
    }

    @Override
    public JSONObject getCompanyByUserId(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String userId = jsonParams.getString("userId");
        String key = jsonParams.getString("key");
        StringBuffer condition=new StringBuffer();
        if (StringUtils.isNotBlank(userId) && StringUtils.isNotEmpty(userId)) {
            condition.append("AND t_sys_company.ID in(\n" +
                    "SELECT\n" +
                    "t_sys_depart.COMPANY_ID AS companyId\n" +
                    "FROM\n" +
                    "t_sys_user\n" +
                    "LEFT JOIN t_sys_role ON t_sys_role.ID=t_sys_user.ROLE_ID\n" +
                    "LEFT JOIN t_sys_depart ON t_sys_depart.ID=t_sys_role.DEPART_ID\n" +
                    "WHERE 1=1\n" +
                    "AND t_sys_user.ID='"+userId+"'\n" +
                    ")\n" );
        }
        if (StringUtils.isNotBlank(key) && StringUtils.isNotEmpty(key)) {
            condition.append("AND (t_wrt_customer.`NAME` LIKE '%"+key+"%'\n" +
                    "OR t_wrt_customer.TEL LIKE '%"+key+"%'\n" + ")\n" );
        }
        List<JSONObject> list=intentcustomerreportDao.getCompanyByUserId(condition.toString());
        JSONObject result = new JSONObject();
        result.put("records",list);
        return result;
    }


}
